$(function () {
    //全局定义页数与总条数
    var page = -1,communityPage = -1,saltedEggPage = -1;
    var totalSize = 0,communityTotalSize = 0,saltedEggTotalSize = 0;
    var communitymRefresh, communityBoolean = true;
    var saltedEggmRefresh, saltedEggBoolean = true;
    var userId = getUrlString('userid'),markId = getUrlString('markid');
    $('#more').click(function () {
        $('.goBlackAlert').show();
    });
    $('.cancel').click(function () {
        $('.goBlackAlert').hide();
    });
    //判断两人是否是好友
    function isFrd() {
        getData('get','/niu/apiv3/weibo_friend.php?method=isMyFriend',{'markid':markId,'friendid':userId},function (data) {
            console.log(data);
            if(data.ismyfriend === '0'){
                $('.addFrd').show().click(function () {
                    window.location.href = 'dancool://fullweb='+urlView+'messagesV2/verifyFrd.html?id='+userId;
                });
            }else{
                $('.addFrd').hide();
            }
        })
    }
    isFrd();
    function getnative(actionurl) {
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isiOS) {
            window.webkit.messageHandlers.getnative.postMessage(actionurl);
        }else {
            ikags.getnative(actionurl)
        }
    }
    //获取用户信息
    function getUserInfo() {
        var data = {
            'markid':markId,
            'userid':userId
        };
        getData('post','/niu/api/weibo_getuserinfo.php',data,function (data) {
            console.log(data);
            if(data.hasOwnProperty('resultcode')){
                laymsg(data.msg)
            }else {

                //判断他人还是自己 1是自己  0是他人
                if(data.ismine === '1'){
                    $('.isLike,#more,#sendMsg,.addFrd').hide();
                    $('.blackList').show();
                }else {
                    $('.blackList').hide();
                    //判断是否在黑名单之列  0不在  1在
                    if(data.inblack === '0'){
                        $('#inblack,#goBackBlack').hide();
                        $('#isLike,#goBlack').show();
                    }else {
                        $('#isLike,#goBlack').hide();
                        $('#inblack,#goBackBlack').show();
                    }
                }

                //发起会话
                $('#sendMsg').click(function () {
                    let tousername = 'djuser_'+userId;
                    getnative('dancool://message_enterSingleConversation='+tousername+'');
                    window.location.href = 'dancool://fullweb='+urlView+'messagesV2/sessionList.html?toUserName='+tousername
                });

                //赋值数据
                $('#faceurl').attr('src',data.faceurl);
                var chenghaoStr = (data.chenghao).substring(3);
                if(chenghaoStr === '0'){
                    $('.rank').hide();
                }

                if (chenghaoStr < 6){
                    $('#chenghaoImg').attr('src','../../res/img/common/bronzeIcon.png')
                }else if (5 < chenghaoStr && chenghaoStr < 11){
                    $('#chenghaoImg').attr('src','../../res/img/common/silverIcon.png')
                }else if (10 < chenghaoStr && chenghaoStr < 16){
                    $('#chenghaoImg').attr('src','../../res/img/common/goldIcon.png')
                }else if (15 < chenghaoStr && chenghaoStr < 21){
                    $('#chenghaoImg').attr('src','../../res/img/common/platinaIcon.png')
                }else if (20 < chenghaoStr && chenghaoStr < 26){
                    $('#chenghaoImg').attr('src','../../res/img/common/diamondIcon.png')
                }
                $('#chenghao').html(data.chenghao);
                $('#set2').html(data.set2);
                $('#guanzhucount').html(data.guanzhucount);
                $('#fancount').html(data.fancount);
                $('#blackcount').html(data.blackcount);
                $('#nick').html(data.nick);

                //判断性别 0男   1女
                if(data.sex === '1'){
                    $('#sex').attr('src','../../res/img/personalCenter/woman.png')
                }else {
                    $('#sex').attr('src','../../res/img/personalCenter/man.png')
                }

                //判断是否关注  0未关注 1关注
                if(data.isfollow === '1'){
                    $('#isfollow').show()
                }else {
                    $('#isNofollow').show()
                }

                //取消关注
                $('#isfollow').click(function () {
                    var data = {
                        'gzuserid':userId,
                        'markid':markId
                    };
                    getData('post','/niu/api/weibo_delfollow.php',data,function (data) {
                        console.log(data);
                        laymsg(data.msg);
                        $('#isfollow').hide();
                        $('#isNofollow').show();
                    })
                });

                //关注某人
                $('#isNofollow').click(function () {
                    var data = {
                        'gzuserid':userId,
                        'markid':markId
                    };
                    getData('post','/niu/api/weibo_dofollow.php',data,function (data) {
                        console.log(data);
                        laymsg(data.msg);
                        $('#isNofollow').hide();
                        $('#isfollow').show();
                    })
                });

                //判断是否是VIP 0不是  1是
                if(data.uservip === '1'){
                    $('#userVip').show();
                }else {
                    $('#userVip').hide();
                }



                //前往黑名单列表
                $('.blackList').click(function () {
                    window.location.href = 'dancool://blacklist='+markId
                });

                //前往私信
                $('#privateLetter').click(function () {
                    window.location.href = 'dancool://talk='+userId+';'+data.nick
                });

                //添加进黑名单
                $('#goBlack').click(function () {
                    var data = {
                        'set':'add',
                        'blockuserid':userId,
                        'markid':markId
                    };
                    getData('post','/niu/api/weibo_doblackuser.php',data,function (data) {
                        console.log(data);
                        // $('.goBlackAlert').slideDown(150);
                        $('.goBlackAlert').hide();
                        laymsg(data.msg);
                        if(data.resultcode == 0){
                            $('#goBlack,#isLike').hide();
                            $('#goBackBlack,#inblack').show();
                        }

                    })
                });
                //移除黑名单
                $('#goBackBlack').click(function () {
                    var data = {
                        'set':'del',
                        'blockuserid':userId,
                        'markid':markId
                    };
                    getData('post','/niu/api/weibo_doblackuser.php',data,function (data) {
                        console.log(data);
                        // $('.goBlackAlert').slideDown(150);
                        $('.goBlackAlert').hide();
                        laymsg(data.msg);
                        $('#goBackBlack,#inblack').hide();
                        $('#goBlack,#isLike').show();
                    })
                })
            }
        })
    }

    getUserInfo();

    //展柜加载
    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#showcaseBoxList').empty();//清空容器
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                myShelves(page);
            }
        }
    });

    //切换tab
    $('.communityBtn').click(function () {
        $('.tabSwitch ul li span').removeClass('active');
        $(this).find('span').addClass('active');
        $('#mrefresh,#saltedEggmRefresh').hide();
        $('#communitymRefresh').show();
        if(communityBoolean){
            //社区加载
            communitymRefresh = new MiniRefresh({
                container: '#communitymRefresh', //这里是插件的容器ID
                down: {
                    isAuto: false,
                    callback: function () {
                        // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                        $('#showCommunityList').empty();//清空容器
                        communityTotalSize = 0;
                        communityPage = -1;
                        communitymRefresh.endDownLoading();
                    }
                }
                ,
                up: {
                    isAuto: true,
                    callback: function () {
                        // 向上拉后的回调事件
                        communityPage += 1;
                        myCommunity(communityPage);
                    }
                }
            });
        }
        communityBoolean = false;
    });
    $('.caseBoxBtn').click(function () {
        $('.tabSwitch ul li span').removeClass('active');
        $(this).find('span').addClass('active');
        $('#communitymRefresh,#saltedEggmRefresh').hide();
        $('#mrefresh').show();
    });
    $('.saltedEggBtn').click(function () {
        $('.tabSwitch ul li span').removeClass('active');
        $(this).find('span').addClass('active');
        $('#mrefresh,#communitymRefresh').hide();
        $('#saltedEggmRefresh').show();
        if(saltedEggBoolean){
            //社区加载
            saltedEggmRefresh = new MiniRefresh({
                container: '#saltedEggmRefresh', //这里是插件的容器ID
                down: {
                    isAuto: false,
                    callback: function () {
                        // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                        $('#saltedEggList').empty();//清空容器
                        saltedEggTotalSize = 0;
                        saltedEggPage = -1;
                        saltedEggmRefresh.endDownLoading();
                    }
                }
                ,
                up: {
                    isAuto: true,
                    callback: function () {
                        // 向上拉后的回调事件
                        saltedEggPage += 1;
                        saltedEggList(saltedEggPage);
                    }
                }
            });
        }
        saltedEggBoolean = false;
    });

    //获取我的社区列表
    function myCommunity(page) {
        var data = {
            'markid':markId,
            'userid':userId,
            'page':page
        };
        getData('post','/niu/apiv3/weibo_getlistv2.php',data,function (data) {
            console.log(data);
            if(data.resultcode == 0){
                myCommunitySetData(data.data,data.pagesize,data.datacount);
            }else {
                laymsg(data.msg);

            }
        })
    }

    //设置我的社区列表数据
    function myCommunitySetData(data,pageSize,datacount) {
        var append = '';
        $(data).each(function () {
            append += '<li>' +
                '                    <input type="hidden" class="id" value="'+this.id+'">' +
                '                    <div class="communityUser">' +
                '                        <div class="communityUserPic">' +
                '                                <img src="'+this.userface+'" alt="">' +
                '                            </div>' +
                '                        <div class="communityUserName">' +
                '                                <p><span>'+this.usernick+'</span></p>' +
                '                                <p>'+subTime(this.time)+'</p>' +
                '                        </div>' +
                '                    </div>' +
                '                    <div class="brief">' +
                '                        <p>'+this.intro+'</p>' +
                '                        <p>' ;
            if(this.picurls != 'null'){
                var picurlsStr = this.picurls;
                var picurlsArr = picurlsStr.split(';');
                for (var j = 0; j < picurlsArr.length;j++){
                    append +=  '<img src="'+picurlsArr[j]+'" alt="">' ;
                }
            }
            append += '</p>' +
                '                    </div>' +
                '                    <div class="operation">' +
                '                        <img src="../../res/img/personalCenter/dz.png" alt="">' +
                '                        <span>'+this.zancount+'</span>' +
                '                        <img src="../../res/img/personalCenter/pl.png" alt="">' +
                '                        <span>'+this.plcount+'</span>' +
                '                    </div>' +
                '                </li>'
        });
        $('#showCommunityList').append(append);

        //总条数
        communityTotalSize = communityTotalSize + pageSize;
        if (communityTotalSize > datacount || data === null)
            communitymRefresh.endUpLoading(true);
        else{
            communitymRefresh.endUpLoading(false);
        }

        $('#showCommunityList li').click(function () {
            var id = $(this).find('input').val();
            window.location.href = 'dancool://web='+urlView+'pullEgg/pullEggDetail.html?id='+id;
            // window.location.href = 'dancool://weibo='+id;
        })
    }

    //获取我的展柜列表
    function myShelves(page) {
        var data = {
            'markid':markId,
            'userid':userId,
            'page':page
        };
        getData('post','/niu/apiv3/niu_getmytoybookv2.php',data,function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                setListData(data.data,data.pagesize,data.datacount);
            }else {
                laymsg(data.msg)

            }
        })
    }

    //设置我的展柜列表数据
    function setListData(data,pageSize,datacount){
        var append = '';
        $(data).each(function () {
            append += '<li>' +
                '           <input type="hidden" value="'+this.toysid+'">'+
                '                    <div class="pic">' +
                '                        <img src="'+this.pics+'" alt="">' +
                '                    </div>' +
                '                    <div class="name">'+this.name+'</div>' +
                '                    <div class="price">'+this.price+'</div>' +
                '                </li>'
        });
        $('#showcaseBoxList').append(append);

        //总条数
        totalSize = totalSize + pageSize;
        if (totalSize > datacount || data === null)
            mRefresh.endUpLoading(true);
        else{
            mRefresh.endUpLoading(false);
        }

        $('#showcaseBoxList li').click(function () {
            var toysid = $(this).find('input').val();
            // window.location.href = 'dancool://toysid='+toysid;
            window.location.href = 'dancool://fullweb='+urlView+'danji/commodityDetail.html?id='+toysid;
        })

    }

    //获取我的咸蛋列表
    function saltedEggList(page) {
        var data = {
            'markid':markId,
            'userid':userId,
            'page':page,
            'userpage':'1'
        };
        getData('post','/niu/apiv3/xiandan_getmylist.php',data,function (data) {
            console.log(data);
            if(data.resultcode == 0){
                mySaltedEggSetData(data.data,data.pagesize,data.datacount);
            }else {
                laymsg(data.msg)

            }
        })
    }


    //设置我的咸蛋列表数据
    function mySaltedEggSetData(data,pageSize,datacount) {
        var append = '';
        $(data).each(function () {
            append += '<li>' +
                '           <input type="hidden" value="'+this.id+'">'+
                '                    <div class="pic">' +
                '                        <img src="'+this.toyspic+'" alt="">' +
                '                    </div>' ;
            if (this.bukuan_status === '0'){
                append += '                    <div class="name"><img src="../../res/img/dj2.png" alt=""><span>'+this.toysname+'</div>' ;
            }else {
                append += '                    <div class="name"><span>'+this.toysname+'</span></div>' ;
            }
                // '                    <div class="name">'+this.toysname+'</div>' +
                // '                    <div class="info">'+this.intro+'</div>' +
            append += '                    <div class="price">' +
                '                        <img style="vertical-align: middle" src="'+this.userface+'" alt="">' +
                '                        <span style="vertical-align: middle" class="nickName">'+this.usernick+'</span>' ;
            if (this.isshow === '1'){
                append +=   ' <span style="color: #ff0000;font-weight: 600;font-size: 0.6rem;vertical-align: middle" class=goldCoinNum>￥'+(this.moneyrmb)/100+'</span>' ;
            } else {
                append +=    '                        <span class="priceNum">'+this.money2+'</span>' +
                    '                        <img class="goldCoin" src="../../res/img/common/goldCoin.png" alt="">'  ;
            }

            append +=  '                    </div>' +
                '                </li>'
        });
        $('#saltedEggList').append(append);

        //总条数
        saltedEggTotalSize = saltedEggTotalSize + pageSize;
        if (saltedEggTotalSize > datacount || data === null)
            saltedEggmRefresh.endUpLoading(true);
        else{
            saltedEggmRefresh.endUpLoading(false);
        }

        $('#saltedEggList li').click(function () {
            var id = $(this).find('input').val();
            window.location.href = 'dancool://fullweb='+urlView+'saltedEggV2/saltedDetail.html?id='+id;
            // window.location.href = 'dancool://xiandan='+id;
        })
    }
});
